#include "stdafx.h"
#include "render/rendermgr.h"
#include "render/3ds/camera.h"

#include "./gamemgr.h"

#define STAGE_SIZE Size(100, 100)
#define STAGE_DEPTH 100

GameMgr::GameMgr()
: m_pMainPage(NULL)
, m_pPlayerModePage(NULL)
, m_pEnemyModePage(NULL)
, m_winSize(0, 0)
{
	RenderMgr::GetInstance().OnStart += EventObject<OnRenderStartFunc>(this, &GameMgr::OnRenderStart);
}

GameMgr::~GameMgr()
{
	delete m_pEnemyModePage;
	m_pEnemyModePage = NULL;

	delete m_pPlayerModePage;
	m_pPlayerModePage = NULL;

	delete m_pMainPage;
	m_pMainPage = NULL;
}

void GameMgr::StartGame(HDC hDC)
{
	RenderMgr::GetInstance().Start(hDC);
}

void GameMgr::StopGame()
{
	RenderMgr::GetInstance().Stop();

	delete m_pPlayerModePage;
	m_pPlayerModePage = NULL;
}

void GameMgr::SetWinSize(Size size)
{
	m_winSize = size;
	RenderMgr::GetInstance().SetWinSize(size);
}

Size GameMgr::GetWinSize()
{
	return m_winSize;
}

Size GameMgr::GetStageSize()
{
	return STAGE_SIZE;
}

float GameMgr::GetStageDepth()
{
	return STAGE_DEPTH;
}

void GameMgr::OnRenderStart()
{
	//set camera
	Camera camera(STAGE_SIZE, STAGE_DEPTH);

	m_pMainPage = new MainPage();
	m_pMainPage->OnPlayerModeClick += EventObject<OnMainPagePlayerModeClickFunc>(this, &GameMgr::OnPlayerModeClick);
	m_pMainPage->OnEnemyModeClick += EventObject<OnMainPageEnemyModeClickFunc>(this, &GameMgr::OnEnemyModeClick);
	m_pMainPage->OnExitClick += EventObject<OnMainPageExitClickFunc>(this, &GameMgr::OnExitClick);

	FireEvent(OnGameReadyFunc, OnReady, ());
}

void GameMgr::ChangeToPage(PageEnum page)
{
	switch (page)
	{
	case PageEnum_MainPage:
		if (m_pPlayerModePage)
		{
			m_pPlayerModePage->OnMenuClick -= EventObject<OnPageBackClickFunc>(this, &GameMgr::OnBackToMenuClick);
			delete m_pPlayerModePage;
			m_pPlayerModePage = NULL;
		}

		if (m_pEnemyModePage)
		{
			m_pEnemyModePage->OnMenuClick -= EventObject<OnPageBackClickFunc>(this, &GameMgr::OnBackToMenuClick);
			delete m_pEnemyModePage;
			m_pEnemyModePage = NULL;
		}

		m_pMainPage->SetVisible(true);
		break;
	case PageEnum_PlayerModePage:
		m_pPlayerModePage = new PlayerModePage();
		m_pMainPage->SetVisible(false);

		m_pPlayerModePage->OnMenuClick += EventObject<OnPageBackClickFunc>(this, &GameMgr::OnBackToMenuClick);

		break;
	case PageEnum_EnemyModePage:
		m_pEnemyModePage = new EnemyModePage();
		m_pMainPage->SetVisible(false);

		m_pEnemyModePage->OnMenuClick += EventObject<OnPageBackClickFunc>(this, &GameMgr::OnBackToMenuClick);

		break;
	}

	RenderMgr::GetInstance().SkipOnce();
}

void GameMgr::OnPlayerModeClick()
{
	ChangeToPage(PageEnum_PlayerModePage);
}

void GameMgr::OnEnemyModeClick()
{
	ChangeToPage(PageEnum_EnemyModePage);
}

void GameMgr::OnExitClick()
{
	FireEvent(OnGameExitFunc, OnExit, ());
}

void GameMgr::OnBackToMenuClick()
{
	ChangeToPage(PageEnum_MainPage);
}